<template>
    <div id="building">
        <div class="ms-title">后台管理系统</div>
        <div class="ms-login">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="账号"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="ruleForm.password" placeholder="密码"></el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submit()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import { Login } from '@/api/admin'
import router from '@/router'
export default {
    data() {
        return {
            ruleForm: {
                username: 'admin',
                password: '123456'
            },
            rules: {
                username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            }
        }
    },
    methods: {
        submit() {
            const data = { "name": this.ruleForm.username, "password": this.ruleForm.password }
            Login(data).then(res => {
                if(res.data.code === 1){
                    sessionStorage.setItem("token",res.data.data.token);
                    sessionStorage.setItem("name",res.data.data.name);
                    localStorage.setItem('username',this.ruleForm.username)
                    router.push('/Info')
                    this.$message.success('登录成功')
                }
                if (res.data.code === 0){
                    this.$message.error(res.data.msg)
                }
            })
        }
    }
}
</script>
<style scoped>
#building {
    background: url("../assets/img/background.jpg");
    width: 100%;
    height: 100%;
    position: fixed;
    background-size: 100% 100%;
}

.ms-title {
    position: absolute;
    top: 50%;
    width: 100%;
    margin-top: -230px;
    text-align: center;
    font-size: 30px;
    font-weight: 600;
    color: #fff;
}

.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 300px;
    height: 160px;
    margin-left: -175px;
    margin-top: -150px;
    padding: 40px;
    border-radius: 5px;
    background: #fff;
}

.login-btn {
    text-align: center;
}

.login-btn button {
    width: 100%;
    height: 36px;
}
</style>
